package com.iap.admin.view.control.code
{
	import com.iap.admin.view.control.FileUpload;
	import com.iap.app.AppGlobal;
	import com.iap.app.util.logger.Log;
	
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	
	import org.osflash.signals.Signal;
	
	import spark.components.VGroup;
	
	public class FileUploadCode extends VGroup
	{
		public function FileUploadCode()
		{
			super();
		}

		private var view:FileUpload;

		public static const EVENT_SELECT:String = "EVENT_SELECT";
		public static const EVENT_COMPLETE:String = "EVENT_COMPLETE";
		public static const EVENT_ERROR:String = "EVENT_ERROR";
		public static const EVENT_URL_ERROR:String = "EVENT_URL_ERROR";
		
		protected var file:FileReference;
		
		protected function init(view:FileUpload):void
		{
			this.view = view;
			file = new FileReference();
			addListeners(file);
		}
		
		private function addListeners(dispatcher:IEventDispatcher):void
		{
			dispatcher.addEventListener(Event.COMPLETE, completeHandler);
			dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			dispatcher.addEventListener(Event.SELECT, selectHandler);
		}
		
		private function removeListeners(dispatcher:IEventDispatcher):void
		{
			dispatcher.addEventListener(Event.COMPLETE, completeHandler);
			dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			dispatcher.addEventListener(Event.SELECT, selectHandler);
		}

		protected function getTypes():Array
		{
			var allTypes:Array = new Array(new FileFilter
				(
					"Images (*.jpg; *.jpeg; *.gif; *.png)",
					"*.jpg; *.jpeg; *.gif; *.png; *.tiff; *.tif;"
				));
			return allTypes;
		}
		
		private function completeHandler(event:Event):void
		{
			removeListeners(file);
			Log.Info("FileUpload.completeHandler: " + event);
			view.progressBar.visible = false;
			dispatchEvent(new Event(EVENT_COMPLETE));
		}
		
		private function ioErrorHandler(event:IOErrorEvent):void
		{
			removeListeners(file);
			Log.Info("FileUpload.ioErrorHandler: " + event + " url="+AppGlobal.FileUploadEndPoint);
			view.progressBar.visible = false;
			dispatchEvent(new Event(EVENT_URL_ERROR));
		}
		
		private function openHandler(event:Event):void
		{
			Log.Info("FileUpload.openHandler: " + event);
		}
		
		private function progressHandler(event:ProgressEvent):void
		{
			var file:FileReference = FileReference(event.target);
			//Log.Info("FileUpload.progressHandler name=" + file.name + " bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
			view.progressBar.visible = true;
		}
		
		private function securityErrorHandler(event:SecurityErrorEvent):void
		{
			Log.Info("FileUpload.securityErrorHandler: " + event);
		}
		
		private function selectHandler(event:Event):void
		{
			Log.Info("FileUpload.selectHandler: name=" + file.name);
			setFileName(file.name);
			sendFile();
			dispatchEvent(new Event(EVENT_SELECT));
		}
		
		public function sendFile():void
		{
			var request:URLRequest = new URLRequest();
			//var params:URLVariables = new URLVariables();
			//request.method = URLRequestMethod.POST;
			//request.data = params;
			request.url = AppGlobal.FileUploadEndPoint;
			file.upload(request,"file");
		}
		
		private function setFileName(fileName:String):void
		{
			view.fileForm.text = fileName;
		}
		
		public function getFileReference():FileReference
		{
			return file;
		}
		
		public function setURLFile(urlFile:String):void
		{
			//view.fileForm.htmlText = "<a href='"+urlFile+"' target='_blank'>"+ urlFile +"</a>";
		}
	}
}